home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Student most wanted Languages / Turbo C v3.0 / Tc3setup.exe / BIN / TRY1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-13  |  1.5 KB  |  105 lines

  1. #include <stdio.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #include <values.h>
  5. #include <time.h>
  6. #include <string.h>
  7.  
  8. const int max=1000;
  9. int list[max];
  10. FILE *fp;
  11.  
  12. void insertion(int aa[],int max1)
  13. {
  14.     int a,b,v;
  15.  
  16.     for(a=1;a<max1;a++)
  17.     {
  18.        v = aa[a];
  19.        b = a;
  20.        while(aa[b-1] > v)
  21.        {
  22.        aa[b] = aa[b-1];
  23.        b = b - 1;
  24.        }
  25.        aa[b] = v;
  26.     }
  27. }
  28.  
  29. void quicksort(int min1,int max2)
  30. {
  31. int t;
  32.  
  33. if (max2 > min1)
  34. {
  35.    int v = list[max2];
  36.    int i = min1 - 1;
  37.    int j = max2;
  38.    do
  39.    {
  40.       do
  41.       {
  42.      i = i + 1;
  43.       } while (list[i] < v);
  44.       do
  45.       {
  46.      j = j - 1;
  47.       } while (list[j] > v);
  48.       int t = list[i];
  49.       list[i] = list[j];
  50.       list[j] = t;
  51.    } while (j > i);
  52.    list[j] = list[i];
  53.    list[i] = list[max2];
  54.    list[max2] = t;
  55.    quicksort(min1,i-1);
  56.    quicksort(i+1,max2);
  57.  }
  58. }
  59.  
  60. void main()
  61. {
  62.  
  63. int i,j,k,l;
  64. char any1[8],any2[8];
  65.  
  66. cout << "Enter a file name\n";
  67. cin >> any1;
  68. /*if(strlen(any1) > 12)
  69.    cout << "File name input error."; */
  70.  
  71.  
  72. fp = fopen(any1,"wb");
  73.     for(j=0;j<30000;j++)
  74.     {
  75.     k = rand();
  76.     fprintf(fp,"%d\n",k);
  77.     }
  78. fclose(fp);
  79. fp = fopen(any1,"rb");
  80.     i = 0;
  81.     while (fscanf(fp,"%d\n",l) != 0)
  82.     {
  83.       list[i] = l;
  84.       i = i + 1;
  85.     }
  86. fclose(fp);
  87.  
  88. /*int min = 0;
  89. quicksort(min,max); */
  90. insertion(list,max);
  91.  
  92. cout << "Enter an output file : ";
  93. cin >> any2;
  94. fp = fopen(any2,"wb");
  95.   for(i=0;i<max;i++)
  96.   {
  97.       int r = list[i];
  98.       fwrite(&r,sizeof(int),1,fp);
  99.   }
  100. fclose(fp);
  101. }
  102.  
  103.  
  104.  
  105.